<?php
namespace App\Http\Controllers\AppApi;

use App\Models\Admin\ComOptionModel;
use App\Models\Main\ComProductModel;

class TestController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 公司、产品关联初始化
     */
    public function setComProInit()
    {
        $coms = ComOptionModel::where('products','<>','')->get();
        if (!count($coms)) { dd('没有数据！'); }
        $dataArr = array();
        foreach ($coms as $com) {
            if ($proStr=$com->products) {
                $pros = json_decode($proStr);
                //针织类
                foreach ($pros->zhens as $k1 => $pro1) {
                    if ($pro1[1]) { //男装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro1[0];
                        $resPro['genre'] = 1;
                        $resPro['type'] = 1;
                        $dataArr[] = $resPro;
                    } else if ($pro1[2]) { //女装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro1[0];
                        $resPro['genre'] = 1;
                        $resPro['type'] = 2;
                        $dataArr[] = $resPro;
                    } else { //童装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro1[0];
                        $resPro['genre'] = 1;
                        $resPro['type'] = 3;
                        $dataArr[] = $resPro;
                    }
                }
                //梭织类
                foreach ($pros->suos as $k2 => $pro2) {
                    if ($pro2[1]) { //男装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro2[0];
                        $resPro['genre'] = 2;
                        $resPro['type'] = 1;
                        $dataArr[] = $resPro;
                    } else if ($pro2[2]) { //女装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro2[0];
                        $resPro['genre'] = 2;
                        $resPro['type'] = 2;
                        $dataArr[] = $resPro;
                    } else { //童装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro2[0];
                        $resPro['genre'] = 2;
                        $resPro['type'] = 3;
                        $dataArr[] = $resPro;
                    }
                }
                //箱包类
                foreach ($pros->baos as $k3 => $pro3) {
                    if ($pro3[1]) { //男装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro3[0];
                        $resPro['genre'] = 3;
                        $resPro['type'] = 1;
                        $dataArr[] = $resPro;
                    } else if ($pro3[2]) { //女装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro3[0];
                        $resPro['genre'] = 3;
                        $resPro['type'] = 2;
                        $dataArr[] = $resPro;
                    } else { //童装
                        $resPro['cid'] = $com->uid;
                        $resPro['proId'] = $pro3[0];
                        $resPro['genre'] = 3;
                        $resPro['type'] = 3;
                        $dataArr[] = $resPro;
                    }
                }
            }
        }
//        dd($dataArr);
        if (!$dataArr) { dd('没有数据！'); }
        ComProductModel::TRUNCATE();
        $resData = array();
        foreach ($dataArr as $item) {
            $comPro = array(
                'cid' => $item['cid'],
                'genre' => $item['genre'],
                'type' => $item['type'],
                'cate' => $item['proId'],
                'created_at' => time(),
            );
            $lastId = ComProductModel::insertGetId($comPro);
            $resData[] = $lastId.'--更新成功';
        }
        dd($resData);
    }
}